package com.ruoyi.framework.shiro.service;

import com.ruoyi.project.lease.customer.domain.Customer;
import com.ruoyi.project.lease.customer.service.ICustomerService;
import com.ruoyi.project.system.dept.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.ShiroConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;

import java.util.Map;

/**
 * 注册校验方法
 * 
 * @author ruoyi
 */
@Component
public class RegisterService
{
    @Autowired
    private IUserService userService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private ICustomerService customerService;

    /**
     * 注册
     */
    public String register(Map<String, String> map)
    {

        User user = new User();
        user.setLoginName(map.get("custPhone"));
        user.setPhonenumber(map.get("custPhone"));
        user.setPassword(map.get("custPassword"));
        user.setUserName(map.get("custName"));
        user.setSex(map.get("custSex"));
        user.setStatus("0");
        user.setDept(deptService.selectDeptById(110L));
        user.setDeptId(110L);
        // 职位设置为客户
        Long[] postIds = { 6L };
        // 角色设为客户角色
        Long[] roleIds = {3L};

        user.setRoleId(3L);
        user.setRoleIds(roleIds);
        user.setPostIds(postIds);

        Customer customer = new Customer();
        customer.setCustId(map.get("custId"));
        customer.setCustName(map.get("custName"));
        customer.setCustPhone(map.get("custPhone"));
        customer.setCustSex(Integer.parseInt(map.get("custSex")));
        customer.setCustAddr(map.get("custAddr"));
        customer.setCustStatus(0);

        String msg = "", loginName = user.getLoginName(), password = user.getPassword();
        String custAddr = customer.getCustAddr(), custId = customer.getCustId(), userName = user.getUserName();

        if (ShiroConstants.CAPTCHA_ERROR.equals(ServletUtils.getRequest().getAttribute(ShiroConstants.CURRENT_CAPTCHA)))
        {
            msg = "验证码错误";
        }
        else if (StringUtils.isEmpty(loginName))
        {
            msg = "手机号/登录名不能为空";
        }
        else if (StringUtils.isEmpty(password))
        {
            msg = "用户密码不能为空";
        }
        else if (StringUtils.isEmpty(custAddr))
        {
            msg = "用户地址不能为空";
        }
        else if (StringUtils.isEmpty(custId))
        {
            msg = "用户身份证号不能为空";
        }
        else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            msg = "密码长度必须在5到20个字符之间";
        }
        else if (loginName.length() < UserConstants.USERNAME_MIN_LENGTH
                || loginName.length() > UserConstants.USERNAME_MAX_LENGTH)
        {
            msg = "账户长度必须在2到20个字符之间";
        }
        else if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(loginName)))
        {
            msg = "保存用户'" + userName + "'失败，手机号" + loginName + "已存在";
        }
        else
        {
            user.setPwdUpdateDate(DateUtils.getNowDate());
            //user.setUserName(loginName);
            boolean regFlag = userService.registerUser(user);
            //int regFlag = userService.insertUser(user);
            if (!regFlag)
            {
                msg = "注册失败,请联系系统管理人员";
            }
            else
            {
                customerService.insertCustomer(customer);
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.REGISTER, MessageUtils.message("user.register.success")));
            }
        }

        return msg;
    }
}
